package com.letme.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letme.common.annotation.DataScope;
import com.letme.common.core.domain.entity.WxUser;
import com.letme.common.utils.SecurityUtils;
import com.letme.common.utils.StringUtils;
import com.letme.store.domain.dto.ModifyWxUserDTO;
import com.letme.store.domain.dto.ModifyWxUserNickDTO;
import com.letme.store.mapper.WxUserMapper;
import com.letme.store.service.IWxUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 微信登录用户Service业务层处理
 *
 * @author lock
 * @date 2024-12-16
 */
@Service
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements IWxUserService
{

    /**
     * 查询微信登录用户列表
     *
     * @param wxUser 微信登录用户
     * @return 微信登录用户
     */
    @Override
    @DataScope(deptAlias = "bwu")
    public List<WxUser> selectWxUserList(WxUser wxUser)
    {
        return this.baseMapper.selectWxUserList(wxUser);
    }

    /**
     * 获取微信登录用户详细信息
     * @param wxUserId
     * @return 微信登录用户对象
     */
    public WxUser selectWxUserByWxUserId(Long wxUserId)
    {
        return this.getOne(new LambdaQueryWrapper<WxUser>().select().eq(WxUser::getWxUserId,wxUserId).eq(WxUser::getDelFlag,0));
    }

    @Override
    public WxUser selectWxUserByOpenId(String openId) {
        return this.getOne(new LambdaQueryWrapper<WxUser>().eq(WxUser::getOpenId,openId).eq(WxUser::getDelFlag,0));
    }


    /**
     * 新增微信登录用户
     *
     * @param wxUser 微信登录用户
     * @return 结果
     */
    @Override
    public Boolean insertWxUser(WxUser wxUser)
    {
        //获取当前的用户登录名
        String userName = SecurityUtils.getUsername();
        if(StringUtils.isNotEmpty(userName)){
            wxUser.setCreateBy(userName);
            wxUser.setUpdateBy(userName);
        }
        wxUser.setDelFlag(0);
        wxUser.setCreateTime(new Date());
        wxUser.setUpdateTime(new Date());
        int rows = this.baseMapper.insert(wxUser);
        return rows > 0;
    }

    @Override
    public Boolean registerWxUser(WxUser wxUser) {
        wxUser.setDelFlag(0);
        wxUser.setCreateTime(new Date());
        wxUser.setUpdateTime(new Date());
        return this.saveOrUpdate(wxUser);
    }

    /**
     * 修改微信登录用户
     *
     * @param wxUser 微信登录用户
     * @return 结果
     */
    @Override
    public Boolean updateWxUser(WxUser wxUser)
    {
        //获取当前的用户登录名
        String userName = SecurityUtils.getUsername();
        if(StringUtils.isNotEmpty(userName)){
            wxUser.setUpdateBy(userName);
        }
        wxUser.setUpdateTime(new Date());
        int rows = this.baseMapper.update(wxUser, new LambdaQueryWrapper<WxUser>().eq(WxUser::getWxUserId, wxUser.getWxUserId()));
        return rows > 0;
    }

    /**
     * 删除微信登录用户信息
     * 逻辑删除，实际还存在
     * @param wxUserIds
     * @return
     */
    @Override
    public Boolean fakeDelWxUserByWxUserIds(Long[] wxUserIds){
        WxUser businessWxUser = new WxUser();
        businessWxUser.setDelFlag(1);
        //获取当前的用户登录名
        String userName = SecurityUtils.getUsername();
        if(StringUtils.isNotEmpty(userName)){
            businessWxUser.setUpdateBy(userName);
        }
        businessWxUser.setUpdateTime(new Date());
        int rows = 0;
        for (Long id:wxUserIds) {
            rows += this.baseMapper.update(businessWxUser, new LambdaQueryWrapper<WxUser>().eq(WxUser::getWxUserId, id));
        }
        return rows > 0;
    }

    /**
     * 删除微信登录用户信息
     * 真删除，删除后数据库不存在
     * @param wxUserIds
     * @return
     */
    public Boolean realDelWxUserByWxUserIds(Long[] wxUserIds){
        int rows = 0;
        for (Long id:wxUserIds) {
            rows += this.baseMapper.delete(new LambdaQueryWrapper<WxUser>().eq(WxUser::getWxUserId,id));
        }
        return rows > 0;
    }


    //--------  小程序端  --------


    /**
     * 查询当前用户的个人信息
     */
    @Override
    public WxUser queryMyInfo() {
        String openId = SecurityUtils.getOpenId();
        return selectWxUserByOpenId(openId);
    }

    /**
     * 编辑用户资料
     * @param dto
     * @return
     */
    @Override
    public Boolean modifyWxUserInfo(ModifyWxUserDTO dto) {
        WxUser wxUser = new WxUser();
        BeanUtils.copyProperties(dto, wxUser);
        return updateWxUserByOpenId(wxUser);
    }

    /**
     * 编辑用户头像，昵称
     * @param dto
     * @return
     */
    @Override
    public Boolean modifyWxUserNick(ModifyWxUserNickDTO dto) {
        WxUser wxUser = new WxUser();
        BeanUtils.copyProperties(dto, wxUser);
        return updateWxUserByOpenId(wxUser);
    }

    //根据微信唯一标识更改用户信息
    private Boolean updateWxUserByOpenId(WxUser wxUser){
        String openId = SecurityUtils.getOpenId();
        wxUser.setUpdateTime(new Date());
        return this.update(wxUser,new LambdaQueryWrapper<WxUser>().eq(WxUser::getOpenId,openId));
    }
}
